--[[

创建一个横向选择面板，在里面添加选择按钮 

[] [] []

]]
local function get_real_x(index,max,w,gap)
    local gap = gap or 50
    local x0 = (1920 - max*w - (max-1)*gap)/2
    return x0 + (index-1)*(w+gap)
end

class.select_panel = extends(class.panel) {
    animation_time = 0,
    new = function (parent,name)

        local panel = class.select_panel:builder 
        {
            parent = parent,
            x = 0,
            y = 0,
            w = 1920,
            h = 1080,
            normal_image = '',
            name = name,
        }
        return panel
    end,
    init = function (self)
        class.panel.init(self)
        
        return self
    end,

    --设置选择按钮
    set_select_button = function (self, count,class_type,select_w,select_h,gap)
        
        self.childs = {}
        self.count = count

        for a=1,count do
            local x = get_real_x(a,count,select_w,gap or 50)
            local button = self:add(class_type,x,200,select_w,select_h,1)
            button.button_type = '选择按钮'
            button.index = a
            self.childs[a] = button
        end
    end,


    --显示数据
    set_data = function(self,list,time)
        self:show()
        for a,button in ipairs(self.childs) do
            local data = list[a]
            if data then
                local x = get_real_x(a,#list,button.w,50)
                button:set_position(x,button.y)
                button:set_data(data)
                button:show()
                if button:has_mouse() then
                    button:notify('on_button_mouse_enter')
                end
            else
                button:hide()
            end
        end
        self.wait_select = ac.clock() + (time or 0)*1000
        self:notify('update')
    end,
    wait_select = 0,
    on_button_clicked = function (self, button)
        if button.index and button.button_type=='选择按钮' then 
            if self.wait_select<ac.clock() then
                ac.sync(self.sync_name or self.name,button.index)
                self:notify('on_select',button)
                self:hide()
            end
        end 
    end,

}

